<?php

namespace core\event;

use app\bll\HookEvent as BllHookEvent;
use ReflectionClass;

class Event
{
    /**
     * 系统发消息（全局）
     *
     * @return void
     */
    public function dispatch($data)
    {

        //获取事件所有的挂载事件并执行
        $bllHookEvent = new BllHookEvent();
        $events = $bllHookEvent->selectByHookIdent($data['ident']);
        
        foreach ($events  as $event) {
            list($eventident, $class, $method) = explode('/', $event['event']);
            $eventclass = '\\component\\event\\' . $eventident . '\\' . $class;
            if (!class_exists($eventclass)) {
                continue;
            }

            $reflectionClass = new ReflectionClass(
                $eventclass
            );

            $method = $reflectionClass->getMethod($method);
            if (!$method->isPublic()) {
                continue;
            }
            $instance = $reflectionClass->newInstance();
            $method->invoke($instance, $data['data']);
        }
    }
}
